package com.lej.my.balance

import androidx.lifecycle.MutableLiveData
import com.lej.core.base.BaseModel
import com.lej.core.base.BaseViewModel
import com.lej.dataservice.club.http.base.getBody
import com.lej.dataservice.club.http.bean.onSuccess
import com.lej.dataservice.club.http.request
import com.lej.my.http.myApiService

/**
 *  author: cjq
 *  date  : 2022/7/7 0007
 *  desc  :
 */
const val page_withdrawal = 0 //提现页
const val page_tips = 1 //规则提示页
const val page_error = 2 //错误提示页
const val page_input = 3 //输入银行信息页

class MyBalanceViewModel : BaseViewModel<BaseModel>() {
    var balanceData: MutableLiveData<String> = MutableLiveData("")
    var pageState: MutableLiveData<Int> = MutableLiveData(0)
    var balanceNum = 0f

    fun getBalanceResult() {
        lifecycleProvider?.let {
            request { myApiService.getBalance() }.onSuccess(it) { itt ->
                val result = itt.get("available_balance").asString ?: "0"
                balanceNum = result.toFloat()
                balanceData.value = "￥${balanceNum / 100f}"
            }
        }
    }

    fun withDraw(num: String, name: String, card: String, bank: String) =
        request {
            myApiService.withDraw(
                getBody(
                    "bank" to bank,
                    "name" to name,
                    "account" to card,
                    "amount" to num
                )
            )
        }

    fun getWithDrawRule(versionName: String) =
        request {
            myApiService.withDrawRule(listOf("withdrawRule"), versionName)
        }
}